<template>
  <div>
    <div v-for="(item, index) in result" :key="index">
      <tpl_block v-if="item.type === 'whiteSpace'" :res="item" />
      <tpl_banner v-if="item.type === 'carousel'" :res="item" />
      <tpl_category
        ref="categoryRef"
        v-if="item.type === 'goodsCategoryFunc'"
        :res="item"
      />
    </div>
  </div>
  <back></back>
</template>

<script setup lang="ts">
import { getPageDataByCode } from '@/api/index'
import tpl_banner from '@/pages/v3-template/tpl_banner.vue' //轮播图
import tpl_block from '@/pages/v3-template/tpl_block.vue' //空白
import tpl_category from '@/pages/v3-template/tpl_category_goods.vue'
import article_item from '@/components/article-item/index.vue'
import {
  onLoad,
  onShow,
  onHide,
  onReachBottom,
  onPageScroll
} from '@dcloudio/uni-app'

import { startRedirectToHome, stopRedirectToHome } from '@/utils/screenSaver'
const result = ref<any>('')
const categoryRef = ref<any>(null)
async function init(functionCode: string) {
  const res = await getPageDataByCode(functionCode)
  console.log('获取装修数据', res)
  let foolData = res.data.data.pageData
    ? JSON.parse(res.data.data.pageData)
    : ''
  if (foolData.version) {
    result.value = foolData.data
    console.log(result.value)
  }
}

const resetTimer = () => {
  startRedirectToHome() // 重置跳转逻辑
}
onMounted(() => {
  startRedirectToHome()
  // 监听用户操作事件
  document.addEventListener('click', resetTimer)
  document.addEventListener('touchstart', resetTimer)
})
onUnmounted(() => {
  stopRedirectToHome()
  // 移除用户操作事件监听
  document.removeEventListener('click', resetTimer)
  document.removeEventListener('touchstart', resetTimer)
})
onLoad((options: any) => {
  init(options.functionCode)
})
onReachBottom(() => {
  console.log('触底函数')
  categoryRef.value.loadMore()
})
onPageScroll((e) => {
  categoryRef.value.pageScroll(e)
})
</script>
<style>
page {
  background: #fff;
}
</style>
